package com.ruicar.afs.cloud.afscase.dispatch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruicar.afs.cloud.afscase.dispatch.DispatchContext;
import com.ruicar.afs.cloud.afscase.dispatch.annotation.Dispatch;
import com.ruicar.afs.cloud.afscase.dispatch.enums.DispatchTypeEnum;
import com.ruicar.afs.cloud.afscase.dispatch.service.DispatchBaseServcie;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseBaseInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.PriorityEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import com.ruicar.afs.cloud.common.rules.RuleHelper;
import com.ruicar.afs.cloud.common.rules.constants.RuleRunEnum;
import com.ruicar.afs.cloud.common.rules.dto.RuleResult;
import com.ruicar.afs.cloud.common.rules.dto.RuleRunResult;
import com.ruicar.afs.cloud.seats.entity.RegularValue;
import com.ruicar.afs.cloud.seats.service.RegularValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cheng.bi
 * 优先级
 */
@Service
@Dispatch(name = "优先级", dependsOn = "dispatchTopLoanService", group = DispatchTypeEnum.LOAN)
public class DispatchPriorLoanService extends DispatchBaseServcie {
    @Autowired
    private CaseBaseInfoService caseBaseInfoService;

    @Autowired
    private CaseContractInfoService caseContractInfoService;

    @Autowired
    private RegularValueService valueService;

    /**
     * 分单前实际处理方法
     *
     * @param context
     */
    public void process(DispatchContext context) {
        List<CaseContractInfo> contractInfoList = (List) (Optional.ofNullable(context.get(LOAN_KEY)).orElse(new ArrayList()));
        List<CaseContractInfo> newContractList=contractInfoList.stream().map(i -> {
            JSONObject jsonObject = (JSONObject) context.get(i.getId());
            RuleRunResult result = RuleHelper.runRule(jsonObject, "loanPriority", false, RuleRunEnum.SERIAL);
            List<Integer> rst = new ArrayList<>();
            if (result.getHit() == true) {
                List<RuleResult> resultList = result.getResults();
                for (RuleResult ruleResult : resultList) {
                    RegularValue regularValue = valueService.getById(ruleResult.getRuleNo());
                    rst.add(Integer.parseInt(regularValue.getValue()));
                }
                /**如果小于正常优先级，不修改优先级*/
                int priorityValue= Integer.valueOf(StringUtils.isNotBlank(i.getPriority())?i.getPriority():"5");
                if(Integer.valueOf(PriorityEnum.ORDINARY.getCode())<=priorityValue){
                    i.setPriority(String.valueOf(Collections.max(rst)));
                }
                caseContractInfoService.updateById(i);
            }
            return i;
        }).sorted(Comparator.comparing(CaseContractInfo::getPriority).reversed()
                .thenComparing(CaseContractInfo::getLendingFirstDate)).collect(Collectors.toList());
        context.put(LOAN_KEY,newContractList);
    }
}
